﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager;
using Manager.App_Code.DTO;
using Manager.App_Code.IDAL;
using Manager.App_Code.DAL;
using Manager.FormMain;
using Manager.App_Code;
using System.Linq;
using Manager.Common;


namespace Manager.FormAdd_Edit
{
    public partial class Frm_Login : DevExpress.XtraEditors.XtraForm
    {
        public String userName;
        public bool IsAuthenticated;
        public static int count;
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();

        public Frm_Login()
        {
            try
            {
                IsAuthenticated = false;
                InitializeComponent();
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_Login: " + ex.Message + ex.StackTrace);
            }
        }

        public Frm_Login(int a)
        {
            IsAuthenticated = false;
            InitializeComponent();
            //frm_MainFlash f = new frm_MainFlash();
            //f.Show();

            //System.Threading.Thread.Sleep(2000);
            //f.Close();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Frm_Login_Load(object sender, EventArgs e)
        {
            //INguoiDung iuser = new NguoiDungDAL();
            //iuser.GetlistNguoiDung();
        }

        private void hyperLinkEdit1_OpenLink(object sender, DevExpress.XtraEditors.Controls.OpenLinkEventArgs e)
        {
            Frm_Connection connect = new Frm_Connection();
            connect.ShowDialog();
        }

        private void spbtnDangNhap_Click(object sender, EventArgs e)
        {            
            try
            {
                var user = db.NguoiDungs.Single(i => i.Username == txtUsername.Text && i.Password == BitConverter.ToString(CommonFunction.encryptDataMD5(txtPass.Text)));
                if (user != null)
                {
                    Form1 frm = new Form1(user);
                    IsAuthenticated = true;
                    this.Hide();
                    frm.Show();

                }
                else
                {
                    MessageBox.Show("Tên hoặc mật khẩu không đúng", "Đăng Nhập", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Đăng Nhập", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}